<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>clicktoeditwrapper_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_editor_clicktoeditwrapper_test.js.html">clicktoeditwrapper_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.editor.ClickToEditWrapperTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.editor.ClickToEditWrapperTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.Range&#39;);
<a name="line20"></a>goog.require(&#39;goog.editor.ClickToEditWrapper&#39;);
<a name="line21"></a>goog.require(&#39;goog.editor.SeamlessField&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.MockClock&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.events&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line25"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line26"></a>goog.require(&#39;goog.userAgent.product&#39;);
<a name="line27"></a>
<a name="line28"></a>var FIELD;
<a name="line29"></a>var CLOCK;
<a name="line30"></a>var HTML;
<a name="line31"></a>
<a name="line32"></a>function setUp() {
<a name="line33"></a>  HTML = goog.dom.getElement(&#39;root&#39;).innerHTML;
<a name="line34"></a>  CLOCK = new goog.testing.MockClock(true);
<a name="line35"></a>
<a name="line36"></a>  // The following 3 lines are to get around an IE bug where it says
<a name="line37"></a>  // &#39;Incompatible markup pointers for this operation&#39;.
<a name="line38"></a>  // Must be done in the setup, not teardown, or else it won&#39;t take effect for
<a name="line39"></a>  // the first test that is run, or any test that runs immediately after a
<a name="line40"></a>  // &quot;breaking async&quot; message from the jsunit framework.
<a name="line41"></a>  goog.dom.Range.clearSelection();
<a name="line42"></a>  window.blur();
<a name="line43"></a>  window.focus();
<a name="line44"></a>}
<a name="line45"></a>
<a name="line46"></a>function setUpField(opt_isBlended) {
<a name="line47"></a>  FIELD = opt_isBlended ? new goog.editor.SeamlessField(&#39;testField&#39;) :
<a name="line48"></a>      new goog.editor.SeamlessField(&#39;testField&#39;);
<a name="line49"></a>
<a name="line50"></a>  (new goog.editor.ClickToEditWrapper(FIELD));
<a name="line51"></a>
<a name="line52"></a>  goog.dom.Range.clearSelection();
<a name="line53"></a>}
<a name="line54"></a>
<a name="line55"></a>function tearDown() {
<a name="line56"></a>  if (FIELD) {
<a name="line57"></a>    FIELD.dispose();
<a name="line58"></a>  }
<a name="line59"></a>
<a name="line60"></a>  CLOCK.dispose();
<a name="line61"></a>
<a name="line62"></a>  goog.dom.getElement(&#39;root&#39;).innerHTML = HTML;
<a name="line63"></a>}
<a name="line64"></a>
<a name="line65"></a>function testClickToEdit(opt_isBlended) {
<a name="line66"></a>  setUpField(opt_isBlended);
<a name="line67"></a>
<a name="line68"></a>  var text = goog.dom.getElement(&#39;testField&#39;).firstChild;
<a name="line69"></a>  goog.dom.Range.createFromNodes(text, 4, text, 8).select();
<a name="line70"></a>
<a name="line71"></a>  goog.testing.events.fireClickSequence(text.parentNode);
<a name="line72"></a>
<a name="line73"></a>  assertFalse(&#39;Field should not be made editable immediately after clicking&#39;,
<a name="line74"></a>      FIELD.isLoaded());
<a name="line75"></a>  CLOCK.tick(1);
<a name="line76"></a>  assertTrue(&#39;Field should be editable&#39;, FIELD.isLoaded());
<a name="line77"></a>
<a name="line78"></a>  var dom = FIELD.getEditableDomHelper();
<a name="line79"></a>  var selection = goog.dom.Range.createFromWindow(dom.getWindow());
<a name="line80"></a>
<a name="line81"></a>  var body = FIELD.getElement();
<a name="line82"></a>  text = body.firstChild;
<a name="line83"></a>
<a name="line84"></a>  assertEquals(&#39;Wrong start node&#39;, text, selection.getStartNode());
<a name="line85"></a>  assertEquals(&#39;Wrong end node&#39;, text, selection.getEndNode());
<a name="line86"></a>  assertEquals(&#39;Wrong start offset&#39;, 4, selection.getStartOffset());
<a name="line87"></a>  assertEquals(&#39;Wrong end offset&#39;, 8, selection.getEndOffset());
<a name="line88"></a>}
<a name="line89"></a>
<a name="line90"></a>function testBlendedClickToEdit() {
<a name="line91"></a>  testClickToEdit(true);
<a name="line92"></a>}
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>function testClickToEditWithAnchor(opt_isBlended) {
<a name="line96"></a>  // We bail out if we are running on chrome+winxp because of flaky selenium
<a name="line97"></a>  // issues. TODO(user): Remove this assertion once we start running on the
<a name="line98"></a>  // JsUnit farm.
<a name="line99"></a>  if (goog.userAgent.product.CHROME &amp;&amp; goog.userAgent.WINDOWS) {
<a name="line100"></a>    return;
<a name="line101"></a>  }
<a name="line102"></a>  setUpField(opt_isBlended);
<a name="line103"></a>
<a name="line104"></a>  goog.dom.getElement(&#39;testAnchor&#39;).focus();
<a name="line105"></a>  goog.testing.events.fireClickSequence(goog.dom.getElement(&#39;testAnchor&#39;));
<a name="line106"></a>
<a name="line107"></a>  CLOCK.tick(1);
<a name="line108"></a>  assertTrue(&#39;Field should be editable&#39;, FIELD.isLoaded());
<a name="line109"></a>
<a name="line110"></a>  var dom = FIELD.getEditableDomHelper();
<a name="line111"></a>  var selection = goog.dom.Range.createFromWindow(dom.getWindow());
<a name="line112"></a>
<a name="line113"></a>  // IE and Gecko and Safari are all dumb and put the cursor
<a name="line114"></a>  // in different places.
<a name="line115"></a>  var body = FIELD.getElement();
<a name="line116"></a>  var text = body.firstChild;
<a name="line117"></a>  var link = dom.getElementsByTagNameAndClass(&#39;A&#39;, null, body)[0].firstChild;
<a name="line118"></a>  var isIEorWebkit = goog.userAgent.WEBKIT || goog.userAgent.IE;
<a name="line119"></a>  assertEquals(&#39;Wrong start node&#39;,
<a name="line120"></a>      isIEorWebkit ? text : link, selection.getStartNode());
<a name="line121"></a>  assertEquals(&#39;Wrong start offset&#39;,
<a name="line122"></a>      isIEorWebkit ? 17 : 0, selection.getStartOffset());
<a name="line123"></a>  assertEquals(&#39;Wrong end node&#39;,
<a name="line124"></a>      isIEorWebkit ? text : link, selection.getEndNode());
<a name="line125"></a>  assertEquals(&#39;Wrong end offset&#39;,
<a name="line126"></a>      isIEorWebkit ? 17 : 0, selection.getEndOffset());
<a name="line127"></a>}
<a name="line128"></a>
<a name="line129"></a>function testBlendedClickToEditWithAnchor() {
<a name="line130"></a>  testClickToEditWithAnchor(true);
<a name="line131"></a>}
</pre>


</body>
</html>
